package sel.me.spring.beans;

import javax.annotation.Resource;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class TestBeanA implements EnvironmentAware{
	
	private Environment environment;
	
	private Long id;
	
	private String name;
	
	 @Value("${factoryBean.flag}")
     private String factoryBeanFlag;
	
//	@Autowired
//	private FactoryBean factoryBean;
	
//	@Resource
//	private FactoryBean factoryBeanOne;
	
	@Resource
	private String factoryBeanTwo;
	
//	@Autowired
//	private ObjectFactory<String> objectFactory;
	
	@Resource
	private ObjectFactory<String> simpleObjectFactory;


	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}
	
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void testFactoryBean() throws Exception {
		System.out.println("============testFactoryBean");
		//spring调用factoryBean.getObject()返回实例
		System.out.println(factoryBeanTwo);
	}
	
	public void testObjectFactory() throws Exception {
		System.out.println("============testObjectFactory");
//		System.out.println(objectFactory.getObject());
		//手动调用objectFactory.getObject()返回实例
		System.out.println(simpleObjectFactory.getObject());
	}
	
	public void testEnvironment() throws Exception {
		String[] defaultProfiles = environment.getDefaultProfiles();
		System.out.println("environment="+ environment);
		System.out.println("environment.getDefaultProfiles()="+String.join(",", defaultProfiles));
		String[] activeProfiles = environment.getActiveProfiles();
		System.out.println("environment.getActiveProfiles()="+String.join(",", activeProfiles));
		//通过 environment 获取到系统属性.
        System.out.println("java.runtime.version="+ environment.getProperty("java.runtime.version"));
        
        //通过 environment 同样能获取到application.properties配置的属性.
        System.out.println("factoryBean.flag="+ environment.getProperty("factoryBean.flag"));
        System.out.println("this.factoryBeanFlag=" + factoryBeanFlag);
//        //获取到前缀是"spring.datasource." 的属性列表值.
//        relaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");
//        System.out.println("spring.datasource.url="+relaxedPropertyResolver.getProperty("url"));
//        System.out.println("spring.datasource.driverClassName="+relaxedPropertyResolver.getProperty("driverClassName"));
	}

}
